﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web_Demo
{
    public partial class AccountSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session[constants.user.ID_ROLE_USER] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (int.Parse(HttpContext.Current.Session[constants.user.ID_ROLE_USER].ToString()) != 1)
            {
                Menu1.Items[3].Enabled = false;

            }
            lbUser.Text = HttpContext.Current.Session[constants.user.FIRST_NAME].ToString();
            if (!Page.IsPostBack)
            {
                loadInfoUser();
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            //dataconnect con = new dataconnect();
            DateTime lstmod = DateTime.Now;
            //string sql1 = "update DMS_User set First_Name = '" + txtFirstName.Text + "', Last_Name = '" + TxtLastName.Text + "',"
            //    + "Email = '" + txtEmail.Text + "', Last_Modified_By_User_ID = 1, Last_Modified_On_Date = '" + lstmod + "' "
            //    +" where ID_User = 1";
            //object a = con.executeNonQuery(sql1);
            int userid = int.Parse(HttpContext.Current.Session[constants.user.ID_USER].ToString());
            DMSdataDataContext dms = new DMSdataDataContext();
            DMS_User accsetting = dms.DMS_Users.Single(p => p.ID_User == userid);
            accsetting.First_Name = txtFirstName.Text;
            accsetting.Last_Name = TxtLastName.Text;
            accsetting.Email = txtEmail.Text;
            accsetting.Last_Modified_By_User_ID = userid;
            accsetting.Last_Modified_On_Date = lstmod;
            dms.SubmitChanges();
            if (accsetting != null)
            {
                Response.Write("<script>alert('Edit successfull')</script>");
                loadInfoUser();
                btedit.Visible = true;
            }
        }
        private void loadInfoUser() 
        {
            
            int iduser = int.Parse(HttpContext.Current.Session[constants.user.ID_USER].ToString());
            DMSdataDataContext dms = new DMSdataDataContext();
            DMS_User UserInfo = dms.DMS_Users.Single(p => p.ID_User == iduser);

            txtFirstName.Text = UserInfo.First_Name;
            TxtLastName.Text = UserInfo.Last_Name;
            txtEmail.Text = UserInfo.Email;
            txtReEmail.Text = UserInfo.Email;

            txtReEmail.Visible = false;
            btSave.Visible = false;
            txtFirstName.Enabled = false;
            TxtLastName.Enabled = false;
            txtEmail.Enabled = false;
            lbremail.Visible = false;
            
            
        }

        protected void lbtlogout_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("remember", "yes");
            cookie.Expires = DateTime.Now;
            Response.Cookies.Add(cookie);
            Response.Redirect("login.aspx");
        }

        protected void btedit_Click(object sender, EventArgs e)
        {
            txtReEmail.Visible = true;
            btSave.Visible = true;
            btedit.Visible = false;
            txtFirstName.Enabled = true;
            TxtLastName.Enabled = true;
            txtEmail.Enabled = true;
            lbremail.Enabled = true;
        }
    }
}